package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exceptions.LyException;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SpecParamServiceImpl extends ServiceImpl<SpecParamMapper, SpecParam> implements SpecParamService {

    //根据复杂条件查询规格参数集合
    @Override
    public List<SpecParamDTO> querySpecParams(Long categoryId, Long groupId, Boolean searching) {
        //健壮性判断
        //关键条件必须要有一个，防止三个条件都缺失的情况下，查空整张表（SELECT * from tb_spec_param）
        if (categoryId == null && groupId == null) {
            throw new LyException(400, "分类和组id不能都为空!");
        }

        //SELECT * from tb_spec_param WHERE category_id = 76 AND group_id = 1 AND searching = 0
        List<SpecParam> specParamList = this.query()
                .eq(categoryId != null, "category_id", categoryId)
                .eq(groupId != null, "group_id", groupId)
                .eq(searching != null, "searching", searching)
                .list();
        List<SpecParamDTO> specParamDTOList = SpecParamDTO.convertEntityList(specParamList);
        return specParamDTOList;
    }

    //新增规格参数
    @Override
    public void saveParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        boolean success = this.save(specParam);
        if (!success){
            throw new LyException(500,"新增规格参数失败！");
        }
    }

    //修改规格参数
    @Override
    public void updateParam(SpecParamDTO specParamDTO) {
        SpecParam specParam = specParamDTO.toEntity(SpecParam.class);
        boolean success = this.updateById(specParam);
        if (!success){
            throw new LyException(500,"修改规格参数失败！");
        }
    }
}